import { http } from "@/utils/http";

type Result = {
  success: boolean;
  data?: { list: Array<any> };
};

type ResultTable = {
  success: boolean;
  data?: {
    /** 列表数据 */
    list: Array<any>;
    /** 总条目数 */
    total?: number;
    /** 每页显示条目个数 */
    pageSize?: number;
    /** 当前页数 */
    currentPage?: number;
  };
};

// 新增后端API返回格式
type ApiResponse<T = any> = {
  code: number;
  data: T;
  message?: string;
};

type UserListResponse = {
  list: Array<any>;
  total: number;
};

/** 获取系统管理-用户管理列表 */
export const getUserList = (data?: object) => {
  return http.request<ApiResponse<UserListResponse>>("get", "/api/admin/users", {
    params: data
  });
};

/** 创建新用户 */
export const createUser = (data: object) => {
  return http.request<ApiResponse<any>>("post", "/api/admin/users", { data });
};

/** 获取用户详情 */
export const getUserDetail = (userId: number) => {
  return http.request<ApiResponse<any>>("get", `/api/admin/users/${userId}`);
};

/** 更新用户信息 */
export const updateUser = (userId: number, data: object) => {
  return http.request<ApiResponse<any>>("put", `/api/admin/users/${userId}`, {
    data
  });
};

/** 重置用户密码 */
export const resetPassword = (userId: number, data: { password: string }) => {
  return http.request<ApiResponse<any>>("post", `/api/admin/users/${userId}/reset-password`, { data });
};

/** 删除用户 */
export const deleteUser = (userId: number) => {
  return http.request<ApiResponse<any>>("delete", `/api/admin/users/${userId}`);
};

/** 为用户分配角色 */
export const assignRolesToUser = (userId: number, data: { role_ids: number[] }) => {
  return http.request<ApiResponse<any>>("post", `/api/admin/users/${userId}/assign_roles`, { data });
};

/** 获取用户当前角色 */
export const getUserRoles = (userId: number) => {
  return http.request<ApiResponse<any>>("get", `/api/admin/users/${userId}/roles`);
};

/** 系统管理-用户管理-获取所有角色列表 */
export const getAllRoleList = () => {
  return http.request<Result>("get", "/api/role/list");
};

/** 系统管理-用户管理-根据userId，获取对应角色id列表（userId：用户id） */
export const getRoleIds = (data?: object) => {
  const userId = (data as any)?.userId;
  return getUserRoles(userId);
};

/** 获取系统管理-菜单管理列表 */
export const getMenuList = (data?: object) => {
  return http.request<Result>("post", "/menu", { data });
};

/** 获取系统管理-部门管理列表 */
export const getDeptList = (data?: object) => {
  return http.request<Result>("post", "/dept", { data });
};

/** 获取系统监控-在线用户列表 */
export const getOnlineLogsList = (data?: object) => {
  return http.request<ResultTable>("post", "/online-logs", { data });
};

/** 获取系统监控-登录日志列表 */
export const getLoginLogsList = (data?: object) => {
  return http.request<ResultTable>("post", "/login-logs", { data });
};

/** 获取系统监控-操作日志列表 */
export const getOperationLogsList = (data?: object) => {
  return http.request<ResultTable>("post", "/operation-logs", { data });
};

/** 获取系统监控-系统日志列表 */
export const getSystemLogsList = (data?: object) => {
  return http.request<ResultTable>("post", "/system-logs", { data });
};

/** 获取系统监控-系统日志-根据 id 查日志详情 */
export const getSystemLogsDetail = (data?: object) => {
  return http.request<Result>("post", "/system-logs-detail", { data });
};

/** 获取角色管理-权限-菜单权限 */
export const getRoleMenu = (data?: object) => {
  return http.request<Result>("post", "/role-menu", { data });
};

/** 获取角色管理-权限-菜单权限-根据角色 id 查对应菜单 */
export const getRoleMenuIds = (data?: object) => {
  return http.request<Result>("post", "/role-menu-ids", { data });
};

/** 获取角色管理-角色管理列表 */
export const getRoleList = (params?: object) => {
  return http.request<ResultTable>("get", "/api/role/list", { params });
};

/** 创建新角色 */
export const createRole = (data: object) => {
  return http.request<ApiResponse<any>>("post", "/api/role/add", { data });
};

/** 更新指定角色信息 */
export const updateRole = (data: object) => {
  return http.request<ApiResponse<any>>("post", "/api/role/update", { data });
};

/** 删除指定角色 */
export const deleteRole = (data: object) => {
  return http.request<ApiResponse<any>>("post", "/api/role/delete", { data });
};

/** 为角色分配权限 */
export const assignPermissionsToRole = (data: object) => {
  return http.request<ApiResponse<any>>("post", "/api/role/assign_permissions", { data });
};

/** 获取所有权限 */
export const getAllPermissions = () => {
  return http.request<ApiResponse<any>>("get", "/api/permission/list");
};

/** 获取角色当前拥有的权限 */
export const getRolePermissions = (role_id: number) => {
  return http.request<ApiResponse<any>>("get", `/api/role/permissions?role_id=${role_id}`);
};
